package mysql

import (
	"database/sql"
	"go_wep_demo/models"
)

func GetTypeList() (typeList []*models.ArticleType, err error) {
	sqlStr := "select type_id,type_name,summary from article_type"
	err = db.Select(&typeList, sqlStr)
	if err != nil {
		if err == sql.ErrNoRows {
			err = nil
		}
	}
	return
}

func CheckTypeExist(id int64) (exist bool) {
	var count int
	sqlStr := "select count(type_id) from article_type where type_id=?"
	err := db.Get(&count, sqlStr, id)
	if err != nil {
		exist = false
		return
	}
	return count > 0
}

func GetTypeNameById(typeId int64) string {
	var name string
	sqlStr := "select type_name from article_type where type_id=?"
	err := db.Get(&name, sqlStr, typeId)
	if err != nil {
		return ""
	}
	return name
}
